package xeroxware.framework;

import javax.servlet.http.HttpServletRequest;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * <pre>
 * RiaArgumentResolver
 * </pre>
 * 
 * @since 2012. 12. 24.
 * @author XEROXER
 */
public class RiaArgumentResolver implements HandlerMethodArgumentResolver {

	private UiAdaptor uiAdaptor;

	public void setUiAdaptor(UiAdaptor adaptor) {
		this.uiAdaptor = adaptor;
	}

	@Override
	public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mavContainer,
			NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {

		Class<?> type = methodParameter.getParameterType();
		Object uiObject = null;

		if (uiAdaptor == null)
			return null;

		if (type.equals(uiAdaptor.getModelName())) {
			HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest();
			uiObject = (Object) uiAdaptor.convert(request);

			return uiObject;
		}

		return null;
	}

	@Override
	public boolean supportsParameter(MethodParameter arg) {
		return true;
	}
}
